package controller

import (
	"github.com/gin-gonic/gin"
	"project/models"
	"project/pkg/app"
	"project/pkg/e"
	"project/services"
)

func Get_Students(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	supplierParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	err, info, total := services.GetAllStudents(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func Add_Students(c *gin.Context) {
	// var m map[string]string
	// b, _ := c.GetRawData()
	// _ = json.Unmarshal(b, &m)
	// sno := m["sno"]
	// sname := m["sname"]
	// ssex := m["ssex"]
	// sage := com.StrTo(m["sage"]).MustInt()
	// sdept := m["sdept"]
	// syear := m["syear"]
	// snation := m["snation"]
	// sarea := m["sarea"]
	// sdegree := com.StrTo(m["sdegree"]).MustInt()
	// spolitics_status := m["spolitics_status"]
	//fmt.Println(sarea)
	sno := c.PostForm("sno")
	sname := c.PostForm("sname")
	ssex := c.PostForm("ssex")
	sage := com.StrTo(c.PostForm("sage")).MustInt()
	sdept := c.PostForm("sdept")
	syear := c.PostForm("syear")
	snation := c.PostForm("snation")
	sarea := c.PostForm("sarea")
	sdegree := com.StrTo(c.PostForm("sdegree")).MustInt()
	spolitics_status := c.PostForm("spolitics_status")
	sdisciplinary_records := c.PostForm("sdisciplinary_records")
	smark := c.PostForm("smark")
	err := services.AddStudents(models.Student{sno, sname, ssex, sage, sdept, syear, snation, sarea, sdegree, spolitics_status, sdisciplinary_records, smark})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")

}

func Update_Students(c *gin.Context) {
	sno := c.PostForm("sno")
	sname := c.PostForm("sname")
	ssex := c.PostForm("ssex")
	sage := com.StrTo(c.PostForm("sage")).MustInt()
	sdept := c.PostForm("sdept")
	syear := c.PostForm("syear")
	snation := c.PostForm("snation")
	sarea := c.PostForm("sarea")
	sdegree := com.StrTo(c.PostForm("sdegree")).MustInt()
	spolitics_status := c.PostForm("spolitics_status")
	if sno == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.UpdateStudents(models.Student{sno, sname, ssex, sage, sdept, syear, snation, sarea, sdegree, spolitics_status, "", ""})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

func Del_Students(c *gin.Context) {
	code := c.PostForm("sno")
	err := services.DelStudents(code)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}